package org.lhz;

import cn.hutool.crypto.SecureUtil;
import com.alibaba.fastjson.JSON;
import org.lhz.entity.BunsBusCustomer;
import org.lhz.service.CustomerService;
import org.lhz.service.impl.CustomerServiceImpl;
import utils.SysTips;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.UUID;

@WebServlet("/foreSystem/user")
public class ForeSystemController extends HttpServlet {
    //注入service对象
    private CustomerService customerService = new CustomerServiceImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        String method = req.getParameter("method");
        //根据请求参数分别处理请求
        switch (method) {
            case "checkUser":
                checkUser(req, resp);
                break;
            case "login":
                login(req, resp);
                break;
            case "register":
                register(req, resp);
                break;
        }
    }

    /**
     * Infor: 验证前台用户名是否重名
     * @param req
     * @param resp
     * @return :
     * @author : LHZ
     * @date : 2021/11/7 10:21
     */
    private void checkUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("loginName");
        BunsBusCustomer cusByCustname = customerService.findCusByCustname(username);
        HashMap<String, Object> map = new HashMap<>();
        if (cusByCustname != null) {
            map.put("exist",true);
            map.put("message","用户名重复，不可用！");
        }else {
            map.put("exist",false);
            map.put("message","用户名可用！");
        }
        //写出数据
        resp.setContentType("application/json;charset=utf-8");
        String json = JSON.toJSONString(map);
        PrintWriter writer = resp.getWriter();
        writer.write(json);
    }

    /**
     * Infor: 前台用户登录
     * @param req
     * @param resp
     * @return : void
     * @author : LHZ
     * @date : 2021/11/7 10:00
     */
    private void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String loginName = req.getParameter("loginName");
        String password = req.getParameter("password");
        String newPassword = SecureUtil.md5(password);
        BunsBusCustomer bunsBusCustomer = new BunsBusCustomer();
        bunsBusCustomer.setCustname(loginName);
        bunsBusCustomer.setPassword(newPassword);
        BunsBusCustomer customer = customerService.findCusByUsernameAndPassword(bunsBusCustomer);
        if (customer != null) {
            //这里把customer写入到session中，前台需要拿到这个数据进行展示，customer.realname
            req.getSession().setAttribute("customer",customer);
            resp.sendRedirect(req.getContextPath()+"/index.jsp");
        }else{
            req.setAttribute("errorMsg", SysTips.USER_LOGIN_ERROR_MSG);
            req.getRequestDispatcher("/login.jsp").forward(req,resp);
        }
    }

    /**
     * Infor: 用户注册
     * @param req
     * @param resp
     * @return : void
     * @author : LHZ
     * @date : 2021/11/7 10:21
     */
    private void register(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("loginName");
        String phone = req.getParameter("phone");
        String password = req.getParameter("password");
        BunsBusCustomer customer = new BunsBusCustomer();
        customer.setIdentity(UUID.randomUUID().toString().replace("-",""));
        customer.setPassword(password);
        customer.setCustname(username);
        customer.setPhone(phone);
        HashMap<String, Object> map = new HashMap<>();
        //验证验证码
            if (customerService.addNewCustomer(customer)>0) {
                //注册成功前台直接重定向到登录页面
                map.put("message","注册成功！");

            }else{
                //注册失败继续停留在注册页面
                map.put("message","注册失败！");
            }
        //写出数据
        resp.setContentType("application/json;charset=utf-8");
        String json = JSON.toJSONString(map);
        PrintWriter writer = resp.getWriter();
        writer.write(json);
        }
    }


